﻿Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Threading.Tasks

Namespace Authorization
    Public Class AuthRepository
        Implements IDisposable
        Private _ctx As AuthContext

        Private _userManager As UserManager(Of IdentityUser)

        Public Sub New()
            _ctx = New AuthContext()
            _userManager = New UserManager(Of IdentityUser)(New UserStore(Of IdentityUser)(_ctx))
        End Sub

        Public Async Function RegisterUser(userModel As UserModel) As Task(Of IdentityResult)

            Dim user As New IdentityUser() With {.UserName = userModel.UserName}
            Dim result = Await _userManager.CreateAsync(user, userModel.Password)

            Return result
        End Function

        Public Async Function FindUser(userName As String, password As String) As Task(Of IdentityUser)
            Dim user As IdentityUser = Await _userManager.FindAsync(userName, password)

            Return user
        End Function

        Public Sub Dispose()
            _ctx.Dispose()
            _userManager.Dispose()

        End Sub

        Public Sub IDisposable_Dispose() Implements IDisposable.Dispose
            _ctx.Dispose()
            _userManager.Dispose()
        End Sub
    End Class
End NameSpace